package com.alan.netty.io;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author weiBin
 * @date 2018/11/5
 */
public class Client {
    public static void main(String[] args) {
        PrintWriter printWriter;
        BufferedReader reader;
        try {
            Socket socket = new Socket("127.0.0.1", 8081);
            printWriter = new PrintWriter(socket.getOutputStream(), true);
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("请输入消息[输入\"Quit\"]退出：");
            String userInput = null;
            while ((userInput = stdIn.readLine()) != null) {
                printWriter.println(userInput);
                System.out.println(reader.readLine());
                if ("Quit".equals(userInput)) {
                    System.out.println("关闭客户端!");
                    printWriter.close();
                    reader.close();
                    stdIn.close();
                    socket.close();
                    System.exit(1);
                    break;
                }
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: PallaviÕs MacBook Pro.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for "
                    + "the connection to: PallaviÕs MacBook Pro.");
            System.exit(1);
        }
    }
}
